package cs4280.ass2.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import cs4280.ass2.entity.House;
import cs4280.ass2.entity.Schedule;
import cs4280.ass2.entity.ScheduleSeatDetail;
import cs4280.ass2.entity.ScheduleTO;
import cs4280.ass2.entity.User;
import cs4280.ass2.persistence.HouseDao;
import cs4280.ass2.persistence.ScheduleDao;
import cs4280.ass2.persistence.ScheduleSeatDetailDao;
import cs4280.ass2.util.httpUtils;

public class SeatPlanAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String scheduleIdStr = req.getParameter("scheduleId");
		String seatDetailId = req.getParameter("seatDetailId");

		if (StringUtils.isNotBlank(seatDetailId)) {
			// select seat.
			ScheduleSeatDetailDao.getInstance().updateSeatStatus(
					Long.valueOf(seatDetailId),
					ScheduleSeatDetail.SEAT_STATUS_PROCESSING,
					req.getSession().getId(), httpUtils.getLoginUserId(req));
			resp.sendRedirect("seatPlan.do?scheduleId=" + scheduleIdStr);
			return;
		}

		if (NumberUtils.isDigits(scheduleIdStr)) {
			long scheduleId = NumberUtils.toLong(scheduleIdStr);
			ScheduleTO scheduleTO = ScheduleDao.getInstance()
					.retrieveScheduleTOById(scheduleId);
			List<ScheduleSeatDetail> seatDetailList = ScheduleSeatDetailDao
					.getInstance().retrieveSeatListBySchedule(scheduleId);

			req.setAttribute("seatDetailList", seatDetailList);
			req.setAttribute("scheduleTO", scheduleTO);
			getServletContext().getRequestDispatcher("/seatPlan.jsp").forward(
					req, resp);
		} else {
			resp.sendError(HttpServletResponse.SC_BAD_REQUEST,
					"invalid scheduleId");
		}
	}
}
